package com.kt.emall.order.service.impl;

import com.kt.common.vo.MemberRespVO;
import com.kt.emall.order.feign.CartFeignService;
import com.kt.emall.order.feign.MemberFeignService;
import com.kt.emall.order.interceptor.UserLoginInterceptor;
import com.kt.emall.order.vo.MemberAddressVO;
import com.kt.emall.order.vo.OrderConfirmVO;
import com.kt.emall.order.vo.OrderItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kt.common.utils.PageUtils;
import com.kt.common.utils.Query;

import com.kt.emall.order.dao.OrderDao;
import com.kt.emall.order.entity.OrderEntity;
import com.kt.emall.order.service.OrderService;


/**
 * @author KamTang
 */
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    MemberFeignService memberFeignService;

    @Autowired
    CartFeignService cartFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public OrderConfirmVO confirmOrder() {
        OrderConfirmVO confirmVO = new OrderConfirmVO();
        MemberRespVO memberRespVO = UserLoginInterceptor.loginUser.get();
        // 1、远程查询所有的收获地址
        List<MemberAddressVO> address = memberFeignService.getAddress(memberRespVO.getId());
        confirmVO.setAddress(address);

        // 2、远程查询购物车所有选中的购物项
        List<OrderItemVO> currentUserCartItem = cartFeignService.getCurrentUserCartItem();
        confirmVO.setItems(currentUserCartItem);
        // feign在远程调用之前要构造请求，调用很多拦截器
        // RequestInterceptor interceptor : requestInterceptors

        // 3、查询用户积分
        Integer integration = memberRespVO.getIntegration();
        confirmVO.setIntegration(integration);

        // 4、其他数据自动计算

        // TODO 5、防重令牌
        return confirmVO;
    }

}
